<!--
 * @Description: 编辑宴席套餐
 * @Author: xiao li
 * @Date: 2021-11-18 10:47:05
 * @LastEditTime: 2022-01-25 11:52:02
 * @LastEditors: xiao li
-->

<template>
  <div class="lb-car-edit">
    <top-nav :title="navTitle" :isBack="true" />
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        ref="subForm"
        :rules="subFormRules"
        label-width="120px"
      >
        <el-form-item label="宴席套餐名称" prop="title">
          <el-input
            v-model="subForm.title"
            placeholder="请输入宴席套餐名称"
            maxlength="20"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="套餐价格" prop="price">
          <el-input v-model="subForm.price" placeholder="请输入套餐价格"
            ><template slot="append">元/桌</template></el-input
          >
        </el-form-item>
        <el-form-item label="会员套餐价格" prop="member_price_table">
          <el-input
            v-model="subForm.member_price_table"
            placeholder="请输入会员套餐价格"
            ><template slot="append">元/桌</template></el-input
          >
        </el-form-item>
        <el-form-item label="宴会价格" prop="banquet_price">
          <el-input v-model="subForm.banquet_price" placeholder="请输入宴会价格"
            ><template slot="append">元/场</template></el-input
          >
          <lb-tool-tips>宴会价格是指包含了场地费用的价格</lb-tool-tips>
        </el-form-item>
        <el-form-item label="会员宴会价格" prop="member_price_site">
          <el-input
            v-model="subForm.member_price_site"
            placeholder="请输入会员宴会价格"
            ><template slot="append">元/场</template></el-input
          >
        </el-form-item>
        <el-form-item label="封面图" prop="cover">
          <lb-cover
            :fileList="subForm.cover"
            @selectedFiles="getCover($event, 'subForm', 'cover')"
          ></lb-cover>
          <lb-tool-tips>图片建议尺寸：254 * 222</lb-tool-tips>
        </el-form-item>
        <el-form-item label="套餐说明" prop="text">
          <el-input
            type="textarea"
            v-model="subForm.text"
            :rows="10"
            resize="none"
            maxlength="300"
            show-word-limit
            placeholder="请输入套餐说明"
          ></el-input>
        </el-form-item>
        <el-form-item label="宴席菜品内容" prop="package_goods">
          <el-tag @click="toShowDialog" style="cursor: pointer"
            >新增菜品</el-tag
          >
          <el-table
            :data="subForm.package_goods"
            :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
            tooltip-effect="dark"
            class="mt-lg"
            style="width: 100%"
          >
            <el-table-column prop="goods_name" label="菜品名称" min-width="120">
            </el-table-column>
            <el-table-column prop="goods_cover" label="菜品图片">
              <template slot-scope="scope">
                <lb-image :src="scope.row.goods_cover" />
              </template>
            </el-table-column>
            <el-table-column prop="link" label="菜品视频" min-width="120">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <div class="table-operate">
                  <lb-button
                    size="mini"
                    plain
                    type="primary"
                    @click="toShowDialog(scope.row)"
                    >{{ $t('action.edit') }}</lb-button
                  >
                  <lb-button
                    size="mini"
                    plain
                    type="danger"
                    @click="toDel(scope.$index)"
                    >{{ $t('action.delete') }}</lb-button
                  >
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="排序值" prop="top">
          <el-input-number
            class="lb-input-number"
            v-model="subForm.top"
            :controls="false"
            :precision="0"
            :min="0"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitInfo">{{
            $t('action.submit')
          }}</lb-button>
          <lb-button @click="$router.back(-1)">{{
            $t('action.back')
          }}</lb-button>
        </el-form-item>
      </el-form>

      <el-dialog
        :title="dialogForm.preId ? '编辑菜品' : '新增菜品'"
        :visible.sync="showDialog"
        width="600px"
        center
      >
        <el-form
          class="dialog-form"
          :model="dialogForm"
          ref="dialogForm"
          :rules="dialogFormRules"
          label-width="100px"
        >
          <el-form-item label="菜品名称" prop="goods_name">
            <el-input
              v-model="dialogForm.goods_name"
              maxlength="20"
              show-word-limit
              placeholder="请输入菜品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="菜品图片" prop="goods_cover">
            <lb-cover
              :fileList="dialogForm.goods_cover"
              @selectedFiles="getCover($event, 'dialogForm', 'goods_cover')"
            ></lb-cover>
            <lb-tool-tips>图片建议尺寸：254 * 222</lb-tool-tips>
          </el-form-item>
          <el-form-item label="菜品视频" prop="link">
            <div class="item-warp">
              <div class="upload-file-warp">
                <input
                  type="text"
                  class="choice-file-input"
                  v-model="dialogForm.link"
                  placeholder="请输入视频链接或选择视频文件"
                />
                <lb-cover
                  type="button"
                  fileType="video"
                  @selectedFiles="getVideo"
                ></lb-cover>
              </div>
            </div>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="submitDialogFormInfo"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reg } from '@/utils/reg'
export default {
  components: {},
  data () {
    return {
      appPath: '',
      navTitle: '',
      base_company: [],
      subForm: {
        id: 0,
        title: '',
        cover: [],
        price: '',
        banquet_price: '',
        member_price_table: '',
        member_price_site: '',
        package_goods: [],
        text: '',
        top: 0
      },
      subFormRules: {
        title: { required: true, validator: reg.isNotNull, text: '宴席套餐名称', reg_type: 2, trigger: 'blur' },
        price: { required: true, validator: reg.isMoney, text: '套餐价格', max_num: 999999.99, trigger: 'blur' },
        banquet_price: { required: true, validator: reg.isMoney, text: '宴会价格', max_num: 999999.99, trigger: 'blur' },
        member_price_table: { required: true, validator: reg.isMoney, text: '会员套餐价格', max_num: 999999.99, trigger: 'blur' },
        member_price_site: { required: true, validator: reg.isMoney, text: '会员宴会价格', max_num: 999999.99, trigger: 'blur' },
        cover: { required: true, type: 'array', message: '请上传封面图', trigger: 'blur' },
        text: { required: true, type: 'string', message: '请输入套餐说明', trigger: 'blur' },
        package_goods: { required: true, type: 'array', message: '请新增宴席菜品内容', trigger: 'blur' },
        top: { required: true, type: 'number', message: '请输入排序值', trigger: 'blur' }
      },
      showDialog: false,
      dialogForm: {
        id: 0,
        preId: '',
        goods_name: '',
        goods_cover: [],
        link: ''
      },
      dialogFormRules: {
        goods_name: { required: true, validator: reg.isNotNull, text: '菜品名称', reg_type: 2, trigger: 'blur' },
        goods_cover: { required: true, type: 'array', message: '请上传菜品图片', trigger: 'blur' }
      }
    }
  },
  async created () {
    let { path } = this.$route
    this.appPath = path.includes('app/') ? '/app' : ''
    let { id } = this.$route.query
    this.navTitle = this.$t(id ? 'menu.BanquetListEdit' : 'menu.BanquetListAdd')
    await this.getBaseInfo()
    if (id) {
      this.subForm.id = id
      this.getDetail()
    }
  },
  methods: {
    async getBaseInfo () {
      let { code, data } = await this.$api.getBaseCompany()
      if (code !== 200) return
      this.base_company = data
    },
    /**
     * @method: 详情
     */
    async getDetail () {
      let { id } = this.subForm
      let { code, data } = await this.$api.banquet.packageInfo({ id })
      if (code !== 200) return
      data.cover = [{ url: data.cover }]
      data.package_goods.map(item => {
        item.preId = item.id
      })
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
    },
    getCover (img, form, key) {
      this[form][key] = img
    },
    getVideo (video) {
      this.dialogForm.link = video[video.length - 1].url
    },
    toShowDialog (item = { id: 0, preId: 0, goods_name: '', goods_cover: [], link: '' }) {
      item = JSON.parse(JSON.stringify(item))
      item.goods_cover = item.goods_cover ? [{ url: item.goods_cover }] : []
      for (let key in this.dialogForm) {
        this.dialogForm[key] = item[key]
      }
      this.showDialog = !this.showDialog
    },
    submitDialogFormInfo () {
      let flag = true
      this.$refs[`dialogForm`].validate(valid => {
        if (!valid) flag = false
      })
      if (flag) {
        let data = JSON.parse(JSON.stringify(this.dialogForm))
        let goods = JSON.parse(JSON.stringify(this.subForm.package_goods))
        let { preId = 0 } = data
        data.goods_cover = data.goods_cover[0].url
        let index = preId ? goods.findIndex(item => {
          return item.preId === preId
        }) : -1
        if (index === -1) {
          data.id = 0
          data.preId = moment(new Date().getTime()).unix()
          goods.push(data)
        } else {
          goods[index] = data
        }
        this.subForm.package_goods = goods
        this.showDialog = false
      }
    },
    toDel (index) {
      this.subForm.package_goods.splice(index, 1)
    },
    /**
     * @method: 新增/编辑
     */
    submitInfo () {
      let flag = true
      this.$refs[`subForm`].validate(valid => {
        if (!valid) flag = false
      })
      if (flag) {
        let subForm = JSON.parse(JSON.stringify(this.subForm))
        if (subForm.price * 1 <= subForm.member_price_table * 1 || subForm.banquet_price * 1 <= subForm.member_price_site * 1) {
          this.$message.error(subForm.price * 1 <= subForm.member_price_table * 1 ? `套餐价格必须大于会员套餐价格` : `宴会价格必须大于会员宴会价格`)
          return
        }
        subForm.cover = subForm.cover[0].url
        let arr = subForm.package_goods.map(item => {
          return { id: item.id, goods_name: item.goods_name, goods_cover: item.goods_cover, link: item.link }
        })
        subForm.package_goods = arr
        let modelMethod = subForm.id ? 'packageUpdate' : 'packageAdd'
        this.$api.banquet[modelMethod](subForm).then(res => {
          if (res.code === 200) {
            this.subForm.id = res.data
            this.$message.success(this.$t(subForm.id ? 'tips.successRev' : 'tips.successSub'))
            this.$router.back(-1)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-car-edit {
  width: 100%;
  .page-main {
    .el-form {
      width: 100%;
      .el-form-item {
        margin-bottom: 24px;
        .el-select,
        .el-input-number,
        .el-input,
        .el-cascader {
          width: 300px;
        }
        .lb-input-number.mini {
          width: 100px;
          margin: 0 10px;
        }
        .el-textarea {
          width: 600px;
        }
        .color-item {
          width: 20px;
          height: 20px;
          border-radius: 5px;
        }
      }
    }
  }
}
</style>
